﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using StyleMVVM.DependencyInjection;
using StyleMVVM.Utilities;
#if DOT_NET

#else
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.Pickers;

#endif

namespace StyleMVVM.View.Impl
{
	public sealed class FauxFilePickerService : IFauxFilePickerService, IFilePickerService
	{
		public static void RegisterExport(IDependencyInjectionContainer container)
		{
			container.Register(typeof(FauxFilePickerService)).
			          As(typeof(IFilePickerService)).
			          As(typeof(IFauxFilePickerService)).
			          InEnvironment(ExportEnvironment.UnitTestOnly | ExportEnvironment.DesignTimeOnly).AndSharedPermenantly();
		}

		public FauxFilePickerService()
		{
			Options = CreationCollisionOption.OpenIfExists;
		}

		public CreationCollisionOption Options { get; set; }

		public string DefaultFileName { get; set; }

		public StorageFile SetCurrentFile { get; set; }

		public IEnumerable<StorageFile> SetCurrentFiles { get; set; }

#if NETFX_CORE
		public IAsyncOperation<IReadOnlyList<StorageFile>> PickMultipleFilesAsync(PickerLocationId location, params string[] filterTypes)
		{
			return InternalPickMultipleFilesAsync(location, filterTypes).AsAsyncOperation();
		}

		public IAsyncOperation<StorageFile> PickFileAsync(PickerLocationId location, params string[] filterTypes)
		{
			return InternalPickFileAsync(location, filterTypes).AsAsyncOperation();
		}
#elif WINDOWS_PHONE
		public Task<IReadOnlyList<StorageFile>> PickMultipleFilesAsync(PickerLocationId location, params string[] filterTypes)
		{
			return InternalPickMultipleFilesAsync(location, filterTypes);
		}

		public Task<StorageFile> PickFileAsync(PickerLocationId location, params string[] filterTypes)
		{
			return InternalPickFileAsync(location, filterTypes);
		}
#elif DOT_NET
		public Task<IReadOnlyList<string>> PickMultipleFilesAsync(PickerLocationId location, params string[] filterTypes)
		{
			throw new NotImplementedException();
		}

		public Task<string> PickFileAsync(PickerLocationId location, params string[] filterTypes)
		{
			throw new NotImplementedException();
		}

		public Task<string> PickSaveFileAsync(PickerLocationId location, params string[] filterTypes)
		{
			throw new NotImplementedException();
		}

#endif

		internal async Task<IReadOnlyList<StorageFile>> InternalPickMultipleFilesAsync(PickerLocationId location,
		                                                                               params string[] filterTypes)
		{
			if (SetCurrentFiles != null)
			{
				return new List<StorageFile>(SetCurrentFiles);
			}

			StorageFile storage = await InternalPickFileAsync(location, filterTypes);
			List<StorageFile> returnValue = new List<StorageFile>();

			if (storage != null)
			{
				returnValue.Add(storage);
			}

			return returnValue;
		}

		internal async Task<StorageFile> InternalPickFileAsync(PickerLocationId location, params string[] filterTypes)
		{
			if (SetCurrentFile != null)
			{
				return SetCurrentFile;
			}

#if DOT_NET
	// TODO
#else
			if (DefaultFileName != null)
			{
				return await ApplicationData.Current.LocalFolder.CreateFileAsync(DefaultFileName, Options);
			}
#endif
			return null;
		}
	}
}